package com.csxh.web.userinfo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.el.util.ReflectionUtil;

import com.csxh.web.userinfo.bean.Pager;
import com.csxh.web.userinfo.bean.User;
import com.csxh.web.userinfo.util.JdbcUtil;
import com.csxh.web.userinfo.util.StringUtil;

/**
 * 用户服务类
 * 
 * @author Administrator
 *
 */
public class UserService {

	// 根据用户名与密码获取用户bean
	public User findUser(String username, String password) {

		User u = null;
		// 在进行数据库操作之前，要进行合法验证,这属性于业务层的逻辑
		if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
			return null;
		}

		// 将查询功能委托dao层进行
		try {
			JdbcUtil.openConnection();
			u = JdbcUtil.selectOne(User.class, "`name`=?", username);

			if (null != u && password.equals(u.getPassword())) {
				return u;
			}

		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}

		return null;
	}

	/**
	 * 根据分页信息查询用户信息
	 * 
	 * @param pager
	 * @return
	 */
	public Pager<User> findUserPager(int pageSize, int pageIndex) {

		Pager<User> pager = new Pager<User>();

		try {
			JdbcUtil.openConnection();
			// 获取总的记录数
			long totalCount = JdbcUtil.countOf("id", "`user`");
			pager = new Pager<User>(totalCount, pageSize, pageIndex);
			// 获取离第一条记录的距离（偏移量）
			long offset = pager.getOffset();
			int count = pageSize;

			StringBuilder sb = new StringBuilder();
			sb.append("select * from `user` limit ").append(offset).append(",").append(count);

			List<User> userList = new ArrayList<User>();
			List<Map<String, Object>> rowMap = JdbcUtil.selectAll(sb.toString());
			for (Map<String, Object> row : rowMap) {

				User u = JdbcUtil.mapToBean(User.class, row);
				userList.add(u);
			}
			pager.setBeanList(userList);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			JdbcUtil.closeConnection();
		}

		return pager;
	}

	/**
	 * 带条件的分页查询,获取最后一页
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param whereClase
	 *            形如：id=? and age>?
	 * @param params
	 * @return
	 */
	public Pager<User> findUserLastPager(Integer pageSize, String whereClase, Object... params) {
		
		Pager<User> pager = new Pager<User>();

		try {
			JdbcUtil.openConnection();
			// 获取总的记录数
			long totalCount = JdbcUtil.countOf("id", "`user`", whereClase, params);
			pager = new Pager<User>(totalCount, pageSize);
			// 将页号定位到最后一页
			pager.locateLast();

			// 获取离第一条记录的距离（偏移量）
			long offset = pager.getOffset();
			int count = pageSize;

			StringBuilder sb = new StringBuilder();
			sb.append("select * from `user` where ").append(whereClase);
			sb.append(" limit ").append(offset).append(",").append(count);

			List<User> userList = new ArrayList<User>();
			List<Map<String, Object>> rowMap = JdbcUtil.selectAll(sb.toString(), params);
			for (Map<String, Object> row : rowMap) {

				User u = JdbcUtil.mapToBean(User.class, row);
				userList.add(u);
			}
			pager.setBeanList(userList);

		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}

		return pager;
	}

	/**
	 * 带条件的分页查询
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param whereClase
	 *            形如：id=? and age>?
	 * @param params
	 * @return
	 */
	public Pager<User> findUserPager(int pageSize, int pageIndex, String whereClase, Object... params) {

		Pager<User> pager = new Pager<User>();

		try {
			JdbcUtil.openConnection();
			// 获取总的记录数
			long totalCount = JdbcUtil.countOf("id", "`user`", whereClase, params);
			pager = new Pager<User>(totalCount, pageSize, pageIndex);
			// 获取离第一条记录的距离（偏移量）
			long offset = pager.getOffset();
			int count = pageSize;

			StringBuilder sb = new StringBuilder();
			sb.append("select * from `user` where ").append(whereClase);
			sb.append(" limit ").append(offset).append(",").append(count);

			List<User> userList = new ArrayList<User>();
			List<Map<String, Object>> rowMap = JdbcUtil.selectAll(sb.toString(), params);
			for (Map<String, Object> row : rowMap) {

				User u = JdbcUtil.mapToBean(User.class, row);
				userList.add(u);
			}
			pager.setBeanList(userList);

		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}

		return pager;
	}

	// 获取所有的用户列表
	public List<User> findUserAll() {
		List<User> userList = new ArrayList<User>();

		try {
			JdbcUtil.openConnection();
			userList = JdbcUtil.selectAll(User.class);
		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}

		return userList;
	}

	public boolean deleteUser(Integer id) {
		try {
			JdbcUtil.openConnection();
			return JdbcUtil.delete("delete from `user` where `id`=" + id);
		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}
		return false;
	}

	public boolean updateUser(User u) {
		try {
			JdbcUtil.openConnection();
			return JdbcUtil.update(u, "user");
		} catch (Exception e) {

		} finally {
			JdbcUtil.closeConnection();
		}
		return false;
	}

	public boolean insertUser(User u) {
		try {
			JdbcUtil.openConnection();
			return JdbcUtil.insert(u, "user");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			JdbcUtil.closeConnection();
		}
		return false;
	}

}
